package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //4.1 学员列表查询
    @GetMapping
    public Result queryStudentList(StudentQueryParam studentQueryParam) {
        log.info("请求参数,{}", studentQueryParam);
        PageResult pageResult = studentService.queryStudentList(studentQueryParam);
        return Result.success(pageResult);
    }

    //4.2 删除学员

    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable String ids) {
        log.info("删除学员,ids {}", ids);
        // 将逗号分隔的字符串转换为List<Integer>
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        studentService.deleteStudent(idList);
        return Result.success();
    }

    //4.3 添加学员
    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        return Result.success();
    }

    //4.4 根据ID查询
    @GetMapping("{id}")
    public Result queryById(@PathVariable Integer id) {

        Student student = studentService.queryById(id);
        return Result.success(student);
    }

    //4.5 修改学员
    @PutMapping
    public Result modifyStudent(@RequestBody Student student) {
        studentService.modifyStudent(student);
        return Result.success();
    }
    //4.6 违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violationHandling(@PathVariable Integer id,@PathVariable Integer score){
        studentService.violationHandling(id,score);
        return Result.success();
    }
}